/**
 * 
 */
package org.okhati.www.sql;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;

import org.okhati.www.model.AddressType;
import org.okhati.www.model.Institution;
import org.okhati.www.model.InstitutionRegistry;

/**
 * @author sujan
 *
 */
public class SqlGenerator {
	
	public static void insertDistrictsSql() {
		InstitutionRegistry registry = InstitutionRegistry.getInstance();
		List<String> districts = registry.getDistricts();
		for (String string : districts) {
			System.out.println("INSERT INTO `places_table` (`name`, `type`) VALUES ('" + string + "', " + AddressType.DISTRICT.getId() + ");");
		}
	}
	
	public static void insertCitiesSql() {
		InstitutionRegistry registry = InstitutionRegistry.getInstance();
		List<String> cities = registry.getCities();
		for (String string : cities) {
			System.out.println("INSERT INTO `places_table` (`name`, `type`) VALUES ('" + string + "', " + AddressType.CITY.getId() + ");");
		}
	}

	public static void insertInstitutionsSql() {
		InstitutionRegistry registry = InstitutionRegistry.getInstance();
		List<Institution> institutions = registry.getInstitutions();
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(new File("institutions.sql"));
			for (Institution institution : institutions) {
				writer.println("INSERT INTO `okhati-core`.`institution_table`"
						+ "(`name`," + "`long_name`," + "`short_name`,"
						+ "`district_name`," + "`region_name`,"
						+ "`city_name`," + "`ward_no`," + "`beds`)"
						+ "VALUES('" + institution.getName() + "', '"
						+ institution.getName() + "', '"
						+ institution.getName() + "', '"
						+ institution.getDistrict() + "', '"
						+ institution.getRegion() + "', '"
						+ institution.getCity() + "', '"
						+ institution.getWard() + "', "
						+ institution.getBedCount() + ");");

			}
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(writer != null) {
				writer.close();
			}
		}
	}

}
